<?php

function list_jurnal_gaji($periode_id=0){
	my_set_code_js('
		function postDataPeriode(periode_id){ 
			location.href=\'index.php?com='.$_GET['com'].'&periode_id=\'+periode_id ;
		}
	');
	if($periode_id==0){
		$periode_id=get_periode_gaji() ;
	}	
	$query_check_periode = " SELECT * FROM pr_periode ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('m-Y',strtotime($prow['tanggal']));
	}

	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
		'onchange'=>'javascript:postDataPeriode(this.value)'
	);
	$src ="<form method=\"GET\">
	<input type=\"hidden\" name=\"com\" value=\"{$_GET['com']}\" />
	<table border=\"0\" width=\"100%\"> <tr><td width='10%'>
	Periode&nbsp;:</td><td width='10%'>".form_dropdown($fperiode_id,$prs)." </td>
	</tr></table></form>";
	
	$header = array(
		'No'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'CCC'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Account'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Description'=>array('style'=>'border-bottom:2px solid;width:40%'),  
		'Date'=>array('style'=>'border-bottom:2px solid;width:17%'),  
		'Debit'=>array('style'=>'text-align:right;border-bottom:2px solid;width:9%'),  
		'Kredit'=>array('style'=>'text-align:right;border-bottom:2px solid;width:9%'),  
	);

	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$month = date('m',strtotime($periode['tanggal']));
	$year = date('Y',strtotime($periode['tanggal']));
	$date='27-'.$month.'-'.$year ;

	$query = "SELECT c.kode_jurnal_gaji, 
		c.nama_jurnal_gaji, e.nama_jabatan,
		f.code, f.label as ccname,
		sum(a.kalkulasi) as total,
		b.tipe , c.dbk
		FROM pr_gaji_komponen a	
		INNER JOIN pr_komponen_gaji b ON a.komponen_id = b.komponen_gaji_id
		INNER JOIN jurnal_gaji c ON c.jurnal_gaji_id = b.jurnal_gaji_id
		INNER JOIN kekaryawanan d ON d.karyawan_id=a.karyawan_id 
		INNER JOIN jabatan e ON e.jabatan_id=d.jabatan_id 
		INNER JOIN pos_biaya f ON f.pos_biaya_id=e.pos_biaya_id 
		WHERE periode_id={$periode_id} AND a.kalkulasi<>0  
		GROUP BY c.kode_jurnal_gaji,e.pos_biaya_id,b.tipe
			ORDER BY c.kode_jurnal_gaji,e.pos_biaya_id,b.tipe 	
	";
	$result = my_query($query);
	$unbalance=$i=0;
	while($ey = my_fetch_array($result) ){
		$i++;
	 	$unbalance+=$ey['total'] ;
		if($ey['dbk'] == 'D'){
			$row[] = array(
				'no'	=> $i,
				'cost'	=> $ey['code'],
				'acc'	=>$ey['kode_jurnal_gaji'],
				'uraian'=> $ey['nama_jurnal_gaji'],
				'date'	=>$date,
				'debet'	=> position_text_align(rp_format(($ey['tipe']==2 ? 0 : $ey['total'])), 'right' ), 
				'kredit'=>  position_text_align(rp_format(0) , 'right' ),   
			); 
		}else{
			$row[] = array(
				'no'	=> $i,
				'cost'	=> $ey['code'],
				'acc'	=>$ey['kode_jurnal_gaji'],
				'uraian'=> $ey['nama_jurnal_gaji'],
				'date'	=>$date,
				'debet'	=> position_text_align(rp_format(0), 'right' ), 
				'kredit'=>  position_text_align(rp_format(($ey['tipe']==2 ? $ey['total']*-1 : 0)), 'right' ),   
			); 
		}
	}
	$accs = my_get_data_by_id('jurnal_gaji','jurnal_gaji_id',1);
	$i++;	
	$row[] = array(
		'no'=> $i,
		'cost'=> $ey['kode_jurnal_gaji'],
		'acc'=>$accs['kode_jurnal_gaji'],
		'uraian'=>$accs['nama_jurnal_gaji'] ,
		'date'=>$date,
		'debet'=> position_text_align(rp_format(($unbalance<0 ? $unbalance*(-1) : 0)), 'right' ),  
		'kredit'=>position_text_align(rp_format(($unbalance<0 ? 0 : $unbalance)), 'right' ),   
	);
	$total_records = $i;
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&periode_id={$_GET['periode_id']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	$pager_url_last = NULL;
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	 
	$n = ($current_page  - 1 ) * 25; 
	$row = array_slice($row,$n,25 );
	
	$datas = table_rows($row);
	$menuontop = array(
		'Excel' => array('onclick'=>'location.href=\'index.php?com='.$_GET['com'].'&periode_id='.$periode_id.'&excel=1\''),
	//	'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=rpt_diklat\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	);
	$printmenu = additional_menu_on_list( $menuontop ,$src);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu.table_builder($header , $datas ,  5 , false ,$paging );
}

function list_jurnal_gaji_excel($periode_id){
	$header = array(
		'No'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Cost Centre'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Account'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Description'=>array('style'=>'border-bottom:2px solid;width:55%'),  
		'Date'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Debit'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Kredit'=>array('style'=>'border-bottom:2px solid;width:15%'),  
	);

	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$month = date('m',strtotime($periode['tanggal']));
	$year = date('Y',strtotime($periode['tanggal']));
	$date='27-'.$month.'-'.$year ;
	$query = "SELECT  f.code, c.nama_jurnal_gaji , c.dbk, c.kode_jurnal_gaji, a.periode_id
		,a.karyawan_id ,a.komponen_id	, a.kalkulasi ,SUM(a.kalkulasi) AS kkalkulasi FROM pr_gaji_komponen a 
		INNER JOIN pr_komponen_gaji b ON a.komponen_id = b.komponen_gaji_id
		INNER JOIN jurnal_gaji c ON b.jurnal_gaji_id = c.jurnal_gaji_id
		INNER JOIN kekaryawanan d ON a.karyawan_id = d.karyawan_id
		INNER JOIN jabatan e ON e.jabatan_id = d.jabatan_id
		INNER JOIN pos_biaya f ON f.pos_biaya_id = e.pos_biaya_id
		WHERE a.periode_id = {$periode_id}
		GROUP BY e.pos_biaya_id , c.jurnal_gaji_id
		ORDER BY c.jurnal_gaji_id , e.pos_biaya_id
	";
	
	$result = my_query($query);
	$unbalance=0;
	$i=0;
	$debet = $kredit= 0;
	while($ey = my_fetch_array($result) ){
		$i++; 
		$unbalance+=$ey['kalkulasi'] ;
		if($ey['dbk'] == 'D'){
			$row[] = array(
				'no'	=>$i,
				'cost'	=> $ey['code'],
				'acc'	=>$ey['kode_jurnal_gaji'],
				'uraian'=> $ey['nama_jurnal_gaji'],
				'date'	=>$date,
				'debet'	=>   $ey['kkalkulasi'] , 
				'kredit'=>   0,   
			); 
			$debet += $ey['kkalkulasi'];
		}else{
			$row[] = array(
				'no'	=> $i,
				'cost'	=> $ey['code'],
				'acc'	=> $ey['kode_jurnal_gaji'],
				'uraian'=> $ey['nama_jurnal_gaji'],
				'date'	=> $date,
				'debet'	=> 0,
				'kredit'=>   ((-1)*$ey['kkalkulasi']  ) ,   
			); 
			$kredit += ((-1)*$ey['kkalkulasi']  );
		}
	}
	
	$query2 = "SELECT f.code ,a.karyawan_id, a.gaji_dibayar FROM pr_gaji a
		INNER JOIN kekaryawanan d ON a.karyawan_id = d.karyawan_id
		INNER JOIN jabatan e ON e.jabatan_id = d.jabatan_id
		INNER JOIN pos_biaya f ON f.pos_biaya_id = e.pos_biaya_id
		WHERE a.periode_id = {$periode_id}
		GROUP BY e.pos_biaya_id  
		ORDER BY   e.pos_biaya_id
	";
	
	$result2 = my_query($query2);
	while($ey2 = my_fetch_array($result2)){  
		$accs = my_get_data_by_id('jurnal_gaji','jurnal_gaji_id',1);
		$i++;	
		$row[] = array(
			'no'=> $i,
			'cost'	=> $ey2['code'],
			'acc'=>$accs['kode_jurnal_gaji'],
			'uraian'=>$accs['nama_jurnal_gaji'] .' '.$month.'-'.$year ,
			'date'=>$date,
			'debet'=>  $ey2['gaji_dibayar']  ,  
			'kredit'=>  0,    
		);
	} 
	
		//$accs = my_get_data_by_id('jurnal_gaji','jurnal_gaji_id',1);
		$i++;	
		$row[] = array(
			'no'=> $i,
			'cost'	=> '9999',
			'acc'=>'21305',
			'uraian'=>'ACC GAJI '.$month.'-'.$year ,
			'date'=>$date,
			'debet'=>  0 ,  
			'kredit'=>  ($debet - $kredit) ,    
		);
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,  24 ,false );
}
 